home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / os2 / pccts.zip / PASCAL.G < prev    next >
Text File  |  1992-12-08  |  7KB  |  308 lines

  1. /*
  2.  * File: pascal.g
  3.  *
  4.  * P a s c a l  G r a m m a r
  5.  *
  6.  * Some of the grammar could be more readable if it were spread out.  However,
  7.  * to show the concise nature of ANTLR, I am squishing it (show off).
  8.  * Theoretically, it would be possible to put the entire grammar into one
  9.  * rule with lots-o-parenthesis and duplicate subrules.
  10.  *
  11.  * This grammar is considered Public Domain and is distributed as is.
  12.  *
  13.  * Terence Parr (c) 1990
  14.  * Purdue University
  15.  * January 1990
  16.  *
  17.  * Modified by Will Cohen 12/17/90
  18.  *
  19.  * scoping is messed up 12/17/90
  20.  */
  21. #header <<
  22.  
  23. #include "sym.h"
  24. #include "pascal.h"
  25. extern Sym *CurSym;
  26.  
  27. >>
  28.  
  29. <<
  30. Sym *CurSym;
  31. static int level = 0;
  32. >>
  33.  
  34. /* made special tokens for key words to simplify lexical analyser */
  35. #token And
  36. #token Array
  37. #token Begin
  38. #token Case
  39. #token Const
  40. #token Div
  41. #token Do
  42. #token Downto
  43. #token Else
  44. #token End
  45. #token File
  46. #token For
  47. #token Forward
  48. #token Function
  49. #token Goto
  50. #token If
  51. #token In
  52. #token Label
  53. #token Mod
  54. #token Nil
  55. #token Not
  56. #token Of
  57. #token Or
  58. #token Packed
  59. #token Procedure
  60. #token Program
  61. #token Record
  62. #token Repeat
  63. #token Set
  64. #token Then
  65. #token To
  66. #token Type
  67. #token Until
  68. #token Var
  69. #token While
  70. #token With
  71.  
  72. /* things to specify identifiers */
  73. #token TypeID
  74. #token UTypeID
  75. #token VarID
  76. #token ProcID
  77. #token ProgID
  78. #token FuncID
  79. #token ConstID
  80.  
  81. /* white space */
  82. #token "[\t\ ]"            << zzskip(); >>
  83. #token "[\n\r]"            << zzline++; zzskip(); >>
  84.  
  85. /* signal start of a comment */
  86. #token "\{"            << zzmode(COMMENT); zzskip();>>
  87. #token "\(\*"            << zzmode(COMMENT); zzskip();>>
  88.  
  89. /* G r o s s  S y n t a x  C o n t r o l */
  90.  
  91.  
  92. program    :    <<Sym *globals=NULL, *p;>>
  93.         Program nonReserved    
  94.         <<make_special_entry(ProgID,$2);
  95.           printf("program %s level %d:\n\n", $2->symbol, level);
  96.            zzs_scope(&globals);
  97.         >>
  98.         "\(" idList "\)"    <<addlist($4,VarID,level);>>
  99.         ";"
  100.         block "."
  101.         <<
  102.           p = zzs_rmscope(&globals);
  103.           printf("global (level %d) symbols:", level);
  104.           for (; p != NULL; p=p->scope)
  105.             printf(" %s", p->symbol);
  106.           printf("\n\n");
  107.         >>
  108.         "@"
  109.     ;
  110.  
  111. block    :    <<Sym **oldscope = zzs_scope(NULL); >>
  112.         decl 
  113.         ( routine <<zzs_scope(oldscope);>> )*
  114.         Begin
  115.             slist
  116.         End
  117.     ;
  118.  
  119. routine    : <<int tok; Sym *p,*locals=NULL;>>
  120.         ( Procedure <<tok=ProcID;>> | Function <<tok=FuncID;>>)
  121.         nonReserved << $2 = make_special_entry(tok,$2);>>
  122.         <<zzs_scope(&locals); level++; >>
  123.         {"\(" parmGroup (";" parmGroup)* "\)"}
  124.         { ":" (TypeID | UTypeID) }
  125.         ";"
  126.         block ";"
  127.         <<
  128.           p = zzs_rmscope(&locals);
  129.           printf("subprogram %s level %d:\n", $2->symbol, --level);
  130.           printf("local (level %d) symbols:", level+1);
  131.           for (; p != NULL; p=p->scope) printf(" %s", p->symbol);
  132.           printf("\n\n");
  133.         >>
  134.     ;
  135.  
  136. parmGroup : {Var} idList ":" (TypeID | UTypeID) <<addlist($2,VarID,level);>> ;
  137.  
  138. slist    : label ( ";" label )* ;
  139.  
  140. label    : {UINT ":"} statement ;
  141.  
  142. statement    : (var | FuncID ) ":=" expr
  143.         | ProcID { "\(" expr ("," expr)* "\)" }
  144.         | Begin slist End
  145.  
  146.         | If expr Then statement { Else statement}
  147.         | Case expr Of
  148.             (constant ("," constant)* ":" statement ";")*
  149.             End
  150.         | While expr Do statement
  151.         | Repeat slist Until expr
  152.         | For VarID ":=" expr ( To| Downto ) expr Do statement
  153.         | With var ( "," var )* Do statement
  154.         | Goto UINT
  155.         | /* empty statement */
  156.         ;
  157.  
  158.  
  159. /* D e c l a r a t i o n  S e c t i o n */
  160.  
  161.  
  162. decl    : ( LABEL UINT ( "," UINT )* ";")*
  163.         { Const nonReserved "=" constant
  164.             <<make_special_entry(ConstID,$2);>>
  165.             ";"
  166.             ( nonReserved "=" constant
  167.                 <<make_special_entry(ConstID,$1);>>
  168.                 ";"
  169.             )*
  170.         }
  171.         { Type nonReserved "=" type
  172.             <<make_special_entry(UTypeID,$2);>>
  173.             ";"
  174.             ( nonReserved "=" type
  175.                 <<make_special_entry(UTypeID,$1);>>
  176.                 ";"
  177.             )*
  178.         }
  179.         { Var idList ":" type ";"    <<addlist($2,VarID,level);>>
  180.             ( idList ":" type ";"    <<addlist($1,VarID,level);>>
  181.             )*
  182.         }
  183.     ;
  184.  
  185. /* tokens than can be redefined in a new scope */
  186. nonReserved    : UTypeID    << $0 = $1; >>
  187.         | VarID        << $0 = $1; >>
  188.         | ProcID    << $0 = $1; >>
  189.         | ProgID    << $0 = $1; >>
  190.         | FuncID    << $0 = $1; >>
  191.         | ConstID    << $0 = $1; >>
  192.         | WORD        << $0 = $1; >>
  193.         ;
  194.  
  195. simpleType    : (TypeID | UTypeID)
  196.         | "\(" wordList <<addlist($2,ConstID,level);>> "\)"
  197.         | constant ".." constant
  198.         ;
  199.  
  200. type        : simpleType
  201.         /* since pointers to types can be defined before actual type*/
  202.         | "^" (TypeID | nonReserved)
  203.         | {Packed} structType
  204.         ;
  205.  
  206. structType    : Array "\[" simpleType ("," simpleType)* "\]" Of type
  207.         | File Of type
  208.         | Set Of simpleType
  209.         | Record fixedPart {";"} (fixedPart {";"} )* {variantPart} End
  210.         ;
  211.  
  212. fixedPart    : idList ":" type
  213.         ;
  214.  
  215. variantPart    : Case {WORD ":"}
  216.             (TypeID | UTypeID) Of variant {";"} (variant {";"})*
  217.         ;
  218.  
  219. variant        : constant ( "," constant )* ":"
  220.             "\(" fixedPart {";"} (fixedPart {";"})* 
  221.             {variantPart} "\)"
  222.         ;
  223.  
  224. /* Use 'link' field of symbol rec to link elements together into idlist */
  225. idList    : <<Sym *list=NULL;>>    /* Return list of id's */
  226.         nonReserved        << $1->link = list; list = $1; >>
  227.         ( "," nonReserved    << $2->link = list; list = $2; >>
  228.         )*
  229.         <<$0 = list;>>
  230.     ;
  231.  
  232. wordList: <<Sym *list=NULL;>>            /* Return list of words */
  233.         WORD        <<$1->link = list; list=$1;>>
  234.         ( "," WORD    <<$2->link = list; list=$2;>>
  235.         )*
  236.                 <<$0 = list;>>
  237.     ;
  238.  
  239. constant : {"\+"|"\-"} (ConstID | UINT { "." UINT {"E" {"\+"|"\-"} UINT}})
  240.     | Nil
  241.     | LITERAL
  242.     ;
  243.  
  244. uconstant : ConstID
  245.     /* floating point number */
  246.     | UINT { "." UINT {"E" {"\+"|"\-"} UINT}}
  247.     | Nil
  248.     | LITERAL
  249.     ;
  250.             
  251.  
  252. /* E x p r e s s i o n  S e c t i o n */
  253.  
  254.  
  255. expr    : simpleExpr (("="|"\<"|"\>"|"\<\>"|"\<="|"\>="| In ) simpleExpr)* ;
  256.  
  257. simpleExpr    : {"\+"|"\-"} term (("\+"|"\-"| Or ) term)* ;
  258.  
  259. term    : factor (("\*"|"/"| Div | Mod | And ) factor)* ;
  260.  
  261. factor    : uconstant
  262.     | var
  263.     | FuncID { "\(" expr ("," expr)* "\)" }
  264.     | "\(" expr "\)"
  265.     | Not factor
  266.     | "\[" {expr {".." expr}} ("," expr {".." expr})* "\]"
  267.     ;
  268.  
  269. var    : VarID ref
  270.     | WORD ref    <</* NOTE error if not defined before use */>>
  271.     ;
  272.  
  273. ref    : "\[" expr ("," expr)* "\]" ref
  274.     | "^" ref
  275.     | "." (WORD <</*NOTE error*/>>|VarID) ref
  276.     |
  277.     ;
  278.  
  279. #token LITERAL "' ~[\0-\31']* '"
  280. #token UINT "[0-9]+"
  281. #token WORD "[a-zA-Z] [a-zA-Z0-9]*"
  282.         <<
  283.         {
  284.             register Sym *p;
  285.  
  286.             p = zzs_get(zzlextext);
  287.             if ( p == NULL ){
  288.                 p = zzs_newadd(zzlextext);
  289.                 p->token = WORD;
  290.             }else{ 
  291.                 zztoken = p->token;
  292.             }
  293.             CurSym = p;
  294.         }
  295.         >>
  296.  
  297. /* special automaton to skip over comments */
  298. #lexclass COMMENT
  299. /* ends of comments */
  300. #token "\*\)"            << zzmode(START); zzskip(); >>
  301. #token "\}"            << zzmode(START); zzskip(); >>
  302.  
  303. #token "[\n\r]"            << zzline++; zzskip(); >>
  304.  
  305. #token "\*"            << zzskip(); >>
  306. #token "~[\*\}\n\r]+"        << zzskip(); >>
  307.  
  308.